package cn.com.nes.site.module.dao.crm;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.Page;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.BaseDaoImpl;

@Repository
public class ProductDaoImpl extends BaseDaoImpl{
	
	private TransactionStatus newg_status = null;
	
	public PageData getProductList(DataReq req) {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取产品信息失败");
        try {
            StringBuffer sb = new StringBuffer();
            StringBuffer sbfrom = new StringBuffer() ;
            sb.append(" select a.uuid,a.productName,b.`value` productType,b.`name` productTypeName,c.`value` productModel,c.`name` productModelName,a.equipmentCompany  " +
					" from product a " +
					" left join (SELECT `name`,`value` from t_param where type_name='产品类型' ) b on a.productType=b.`value` " +
					" left join (SELECT `name`,`value` from t_param where type_name='产品型号' ) c on a.productModel=c.`value` " +
					" where a.isdelete<>'1' ");
            if (!StringUtils.isEmpty((String) param.get("productName"))){
                sb.append(" and a.productName like '").append(param.get("productName")).append("%' ");
            }
            if (!StringUtils.isEmpty((String) param.get("productType"))){
                sb.append(" and a.productType like '").append(param.get("productType")).append("%' ");
            }
            if (!StringUtils.isEmpty((String) param.get("productModel"))){
                sb.append(" and a.productModel like '").append(param.get("productModel")).append("%' ");
            }
            sb.append(" order by a.createTime desc ");
            sb = DBUtils.sql_check(sb) ;
            sbfrom.append(sb.toString()) ;
            int idx=sbfrom.indexOf(" from ") ;
            sbfrom.delete(0, idx);
            Page page = new Page(this.getJdbcTemplate_newg(),sbfrom.toString(),req.getPageid(),req.getPagesize(),res,new Object[]{}) ;
            sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize()) ;
            List<Map<String, Object>> list = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("OK");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return res;
    }
	
	@SuppressWarnings("unchecked")
	public PageData saveProduct(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("参数信息保存失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "product");
		PageData sqlInfo =null;
		String uuid=(String)param.get("uuid");
		try {
			if (uuid ==null || "".equals(uuid)){
				param.put("isdelete", "0");
				param.put("sort", "0");
				uuid = UUID.randomUUID().toString(); 
				sqlInfo = getInsertSQL(req);
			}else{
				param.put("keys", "uuid") ;
				sqlInfo = getUpdateSQL(req);
			}
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			final StringBuffer sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			final List<String> listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
		
			commit_newg(newg_status);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {

		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public PageData delProduct(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("删除信息失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "product");
		param.put("keys", "uuid") ;
		try {
			param.put("isdelete", "1");
			PageData sqlInfo = getUpdateSQL(req);
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			StringBuffer sb_field = null ;
			List<String> listData =  null ;
			
			sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
			
			commit_newg(newg_status);
			res.setKey((String)param.get("uuid")) ;
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {
			
		}
		return res;
	}
	
	public PageData getPriceList(DataReq req) {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取产品信息失败");
        try {
            StringBuffer sb = new StringBuffer();
            StringBuffer sbfrom = new StringBuffer() ;
            sb.append(" select a.uuid,a.priceName,a.priceDetails " +
					" from price a " +
					" where a.isdelete<>'1' ");
            if (!StringUtils.isEmpty((String) param.get("priceName"))){
                sb.append(" and a.priceName like '").append(param.get("priceName")).append("%' ");
            }
            sb.append(" order by a.createTime desc ");
            sb = DBUtils.sql_check(sb) ;
            sbfrom.append(sb.toString()) ;
            int idx=sbfrom.indexOf(" from ") ;
            sbfrom.delete(0, idx);
            Page page = new Page(this.getJdbcTemplate_newg(),sbfrom.toString(),req.getPageid(),req.getPagesize(),res,new Object[]{}) ;
            sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize()) ;
            List<Map<String, Object>> list = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("OK");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return res;
    }
	
	@SuppressWarnings("unchecked")
	public PageData savePrice(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("参数信息保存失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "price");
		PageData sqlInfo =null;
		String uuid=(String)param.get("uuid");
		try {
			if (uuid ==null || "".equals(uuid)){
				param.put("isdelete", "0");
				param.put("sort", "0");
				uuid = UUID.randomUUID().toString(); 
				sqlInfo = getInsertSQL(req);
			}else{
				param.put("keys", "uuid") ;
				sqlInfo = getUpdateSQL(req);
			}
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			final StringBuffer sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			final List<String> listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
		
			commit_newg(newg_status);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {
			
		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public PageData delPrice(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("删除信息失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "price");
		param.put("keys", "uuid") ;
		try {
			param.put("isdelete", "1");
			PageData sqlInfo = getUpdateSQL(req);
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			StringBuffer sb_field = null ;
			List<String> listData =  null ;
			
			sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
			
			commit_newg(newg_status);
			res.setKey((String)param.get("uuid")) ;
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {
			
		}
		return res;
	}
	
	public PageData getProductPriceList(DataReq req) {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取产品信息失败");
        try {
            StringBuffer sb = new StringBuffer();
            StringBuffer sbfrom = new StringBuffer() ;
            sb.append(" select a.uuid,a.price,a.memo,a.moneyRate, " +
            		" b.uuid priceId,b.priceName, " +
            		" c.uuid productId,c.productName,c.equipmentCompany, " +
            		" d.`value` productType,d.`name` productTypeName," +
            		" e.`value` productModel,e.`name` productModelName  " +
					" from product_price a" +
					" left join price b on a.priceId=b.uuid and b.isdelete<>'1' " +
					" left join product c on a.productId=c.uuid and c.isdelete<>'1' " +
					" left join (SELECT `name`,`value` from t_param where type_name='产品类型' ) d on c.productType=d.`value` " +
					" left join (SELECT `name`,`value` from t_param where type_name='产品型号' ) e on c.productModel=e.`value` " +
					" where a.isdelete<>'1' ");
            if (!StringUtils.isEmpty((String) param.get("priceId"))){
                sb.append(" and a.priceId like '").append(param.get("priceId")).append("%' ");
            }
            if (!StringUtils.isEmpty((String) param.get("productId"))){
                sb.append(" and a.productId like '").append(param.get("productId")).append("%' ");
            }
            sb.append(" order by a.createTime desc ");
            sb = DBUtils.sql_check(sb) ;
            sbfrom.append(sb.toString()) ;
            int idx=sbfrom.indexOf(" from ") ;
            sbfrom.delete(0, idx);
            Page page = new Page(this.getJdbcTemplate_newg(),sbfrom.toString(),req.getPageid(),req.getPagesize(),res,new Object[]{}) ;
            sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize()) ;
            List<Map<String, Object>> list = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("OK");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return res;
    }
	
	@SuppressWarnings("unchecked")
	public PageData saveProductPrice(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("参数信息保存失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "product_price");
		PageData sqlInfo =null;
		String uuid=(String)param.get("uuid");
		try {
			if (uuid ==null || "".equals(uuid)){
				param.put("isdelete", "0");
				param.put("sort", "0");
				uuid = UUID.randomUUID().toString(); 
				sqlInfo = getInsertSQL(req);
			}else{
				param.put("keys", "uuid") ;
				sqlInfo = getUpdateSQL(req);
			}
			param.put("price", Double.valueOf((String)param.get("price")));
			param.put("moneyRate", Double.valueOf((String)param.get("moneyRate")));
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			final StringBuffer sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			final List<String> listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
		
			commit_newg(newg_status);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {
			
		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public PageData delProductPrice(DataReq req) {
		Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("删除信息失败");
		newg_status = begin_newg();// 开启事物
		param.put("tableName", "product_price");
		param.put("keys", "uuid") ;
		try {
			param.put("isdelete", "1");
			PageData sqlInfo = getUpdateSQL(req);
			if (sqlInfo.getRetCode() != 0) {
				rollback_newg(newg_status);
				return sqlInfo;
			}
			StringBuffer sb_field = null ;
			List<String> listData =  null ;
			
			sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
			listData = (List<String>) sqlInfo.getData().get("listData");
			this.getJdbcTemplate_newg().update(sb_field.toString(),listData.toArray());
			
			commit_newg(newg_status);
			res.setKey((String)param.get("uuid")) ;
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
			rollback_newg(newg_status);
		} finally {
			
		}
		return res;
	}
	
	public PageData getJglxList(DataReq req) {
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("获取信息失败");
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT a.uuid value,a.priceName name FROM price a ");
			sb.append(" WHERE a.isdelete <> '1' ") ;
			List<Map<String, Object>> list =this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
	
		} finally {

		}
		return res;
	}
	
	public PageData getCpmcList(DataReq req) {
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("获取信息失败");
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT a.uuid value,a.productName name FROM product a ");
			sb.append(" WHERE a.isdelete <> '1' ") ;
			List<Map<String, Object>> list =this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
		
		} finally {

		}
		return res;
	}
	
}
